home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / dirut / retain20.zip / RETAIN.BAT < prev    next >
DOS Batch File  |  1992-12-11  |  15KB  |  346 lines

  1. echo off
  2.  rem ===RETAIN.BAT, vs. 2.0, 11/12-92===
  3. if "%1"=="" echo --- RETAIN 2.0 (c) 1992, Jim Groeneveld; retains current path up to 7 levels.
  4.  rem if no argument at all make first one dummy + to auto-set second one later:
  5. if "%1"=="" %0 +
  6. if "%1"=="?" goto Help
  7. if "%1"=="/?" goto Help
  8. if "%1"=="-?" goto Help
  9. if "%1"==".path" goto Path
  10.  rem if second argument (return-file specification) not given determine it:
  11.  rem (no check on erroneous presence of .BAT extension possible)
  12. if "%2"=="" goto AltRtn
  13. if "%1"==".notfound" goto NoPath
  14. if "%1"=="*" if exist %2.BAT %2 *
  15. if "%1"=="*" if not exist %2.BAT goto NoList
  16. if "%1"==".list" goto List
  17. if "%1"==".recreate" goto Create
  18. if "%1"==".return" goto RETURN
  19. if "%1"==".reversed" goto RETURN
  20. if not "%1"=="+" goto RETAIN
  21.   rem %1 is the location or specification of the auxiliary file $RETURN$.AUX
  22.  
  23. :Determine
  24.  rem Determination of location (and name) of auxiliary file $RETURN$.AUX in/as:
  25.  rem a. the first and only parameter when calling RETAIN (not checked here)
  26.  rem    1. the full _path_ only, incl. trailing '\' (e.g. 'RETAIN C:\UTIL\') or
  27.  rem    2. the full _file_specification_ (e.g. 'RETAIN C:\UTIL\AUX-FILE.$$$')
  28.  rem b. the environment variable %RETAIN%
  29.  rem    1. the full _path_ only, incl. trailing '\' ('SET RETAIN=C:\UTIL\') or
  30.  rem    2. the full _file_specification_ ('SET RETAIN=C:\UTIL\AUX-FILE.$$$')
  31.  rem c. the ROOT directory of C: (C:\) (or automatically any APPENDed path) or
  32.  rem d. the ROOT directory of the CURRENT drive (\) or
  33.  rem e. the CURRENT directory (.\) or
  34.  rem f. the PARENT directory of the current one (..\) or
  35.  rem g. the SAME directory where RETAIN.BAT is residing:
  36.  rem    if EXPLICITELY specified when starting RETAIN (DOS 3.x&up only).
  37.  rem    (NOT if RETAIN.BAT can only be found via the PATH!) or
  38.  rem h. the DOS PATH variable (this may take quite a while)
  39.  rem If the environment variable has not been set: try other location(s)
  40. if "%RETAIN%"=="" goto NoEnvVar
  41.  rem If $RETURN$.AUX found as file specified by %RETAIN% then restart this batch
  42.  rem file (RETAIN.BAT as %0) with _full_path_&_file_name_ as the first parameter
  43. if exist %RETAIN% %0 %RETAIN% %2
  44.  rem If $RETURN$.AUX found in path specified by %RETAIN% then restart this batch
  45.  rem file (RETAIN.BAT as %0) with _full_path_&_file_name_ as the first parameter
  46. if exist %RETAIN%$RETURN$.AUX %0 %RETAIN%$RETURN$.AUX %2
  47.  rem If the auxiliary file thus can not be found: try other location(s) below:
  48.  
  49. :NoEnvVar
  50.  rem Check the presence of $RETURN$.AUX in (and specify _full_path_&_file_name_
  51.  rem if found):
  52.  rem (the root of C:)
  53. if exist    C:\$RETURN$.AUX %0    C:\$RETURN$.AUX %2
  54.  rem (the root of the current drive)
  55. if exist      \$RETURN$.AUX %0      \$RETURN$.AUX %2
  56.  rem (the current directory)
  57. if exist     .\$RETURN$.AUX %0     .\$RETURN$.AUX %2
  58.  rem (the parent directory)
  59. if exist    ..\$RETURN$.AUX %0    ..\$RETURN$.AUX %2
  60.  rem (the explicitely specified directory)
  61. if exist %0\..\$RETURN$.AUX %0 %0\..\$RETURN$.AUX %2
  62.  
  63.  rem ---DOSPATH---
  64.  rem Within the subroutine :Path to be called now by subsequent 'shift'ing
  65.  rem the call name for RETAIN (%0) gets lost (as well as the in version 2.0
  66.  rem introduced 2nd parameter, the alternate return file). To solve this
  67.  rem problem a temporary batch file $RETAIN$.BAT is created initially (in
  68.  rem the current directory) that contains this information hardcoded and
  69.  rem calls RETAIN (or whatever it is renamed) with the necessary parameter:
  70. echo echo off>$RETAIN$.BAT
  71.  rem If no write of this auxiliary file is possible then cancel finally:
  72. if not exist $RETAIN$.BAT goto NoPath
  73. echo %0 %%1 %2>>$RETAIN$.BAT
  74.  rem At the end (or somewhere suited) remove the temporary file $RETAIN$.BAT .
  75.  rem Depending on the (path and) name (length) for RETAIN the path environment
  76.  rem variable will be used up to a certain number of characters, maximally
  77.  rem around 100.
  78. %0 .path %path%
  79.  
  80. :NoPath
  81. echo *** Necessary auxilary file $RETURN$.AUX could not be found - abort!
  82. echo.
  83. echo     Specify environment variable RETAIN to point to the path where
  84. echo     $RETURN$.AUX resides (either from the DOS prompt or in AUTOEXEC.BAT).
  85. echo     E.g. SET RETAIN=C:\UTIL\             (terminated with backslash).
  86. echo     Or specify full path and file name to the eventual alternate auxiliary
  87. echo     file for $RETURN$.AUX (e.g. SET RETAIN=C:\UTIL\AUX-FILE.$$$).
  88. echo.
  89. echo     Or specify path (terminated with '\') to or full file specification
  90. echo     for $RETURN$.AUX as the first (and only) parameter for %0
  91. goto end
  92.  
  93. :AltRtn
  94.  rem Determine return-file (if not specified already as the second argument)
  95. if "%RETURN%"=="" %0 %1 C:\RETURN
  96. %0 %1 %RETURN%
  97.  
  98. :RETAIN
  99. if not "%3"=="" goto AuxPath_checked
  100.  rem Now %3 is empty: aux-path[aux-file] %1 specified by user, check existence:
  101.  rem If %1 is _full_path_&_file_name_ of (alternate) $RETURN$.AUX then continue
  102. if exist %1 goto AuxPath_checked
  103.  rem If %1 is path only to explicit $RETURN$.AUX then restart RETAIN (%0) with
  104.  rem _full_path_&_file_name_ as the first parameter
  105. if exist %1$RETURN$.AUX %0 %1$RETURN$.AUX %2
  106.  rem %1 has been specified by the user, but $RETURN$.AUX could not be found:
  107. echo *** Auxiliary path[file] %1 is incorrect, file $RETURN$.AUX not found.
  108. echo     Now trying to find $RETURN$.AUX using path (or full file specification):
  109. echo     1. from environment variable RETAIN (path or full file specification)
  110. echo     2. the root directory of C: (C:\) (or automatically any APPENDed path)
  111. echo     3. the root of the current drive (\)
  112. echo     4. the current directory (.\)
  113. echo     5. the parent directory (..\)
  114. echo     6. the same directory as RETAIN.BAT if RETAIN has been called by path
  115. echo     7. from the DOS PATH variable (this may take quite a while)
  116. echo.
  117. goto Determine
  118.  
  119. :AuxPath_checked
  120.  rem Check for existence of %2.BAT (return-file): if so, nesting has to take
  121.  rem place. This is done by adding the current path parameter after the already
  122.  rem present path parameters in %2.BAT, where it calls RETAIN.
  123. if not exist %2.BAT goto Create
  124.  rem %3 (and succeeding parameters) is the retained drive and subdirectory
  125.  rem If path(s) stored are known resume with recreation of %2.BAT
  126. if not "%3"=="" goto Create
  127.  rem If no stored paths are known (just started RETAIN) force RETURN to
  128.  rem supply the already stored path(s) (to add the current path to).
  129.  rem This returns all currently stored paths as additional parameters up to 7
  130. %2 %0 %1 .retain
  131.  
  132. :Create
  133.  rem This is the part to RETAIN the current drive:path
  134.  rem Creation of %2.BAT (the return-file)
  135. if not "%1"==".recreate" if not "%9"=="" goto ListFull
  136.  rem Initially explicitely delete %2.BAT in order to see if that works:
  137. if exist %2.BAT del %2.BAT
  138. if exist %2.BAT goto NoDelete
  139. echo echo off>%2.BAT
  140. if not exist %2.BAT goto NoReturn
  141. echo rem --- %2.BAT 2.0 (c) 1992, Jim Groeneveld>>%2.BAT
  142. echo if "%%1"=="?" %0 ?>>%2.BAT
  143. echo if "%%1"=="/?" %0 ?>>%2.BAT
  144. echo if "%%1"=="-?" %0 ?>>%2.BAT
  145.  
  146. echo if %%0==%2 goto AllRight>>%2.BAT
  147.  rem Build check on unmoved/unrenamed %2.BAT into itself and
  148.  rem make sure the original return-file is being run (if it exists):
  149.  rem (this makes command lines longer, which may be undesired and unnecessary)
  150. echo if not exist %2.BAT goto NoReturn>>%2.BAT
  151. echo %2 %%1 %%2 %%3>>%2.BAT
  152. echo :NoReturn>>%2.BAT
  153. echo echo *** This file %2.BAT has been moved or renamed. DON'T! Abort!>>%2.BAT
  154. echo echo     Execution of this BATch file %%0 may cause erroneous results.>>%2.BAT
  155. echo goto end>>%2.BAT
  156.  
  157. echo :AllRight>>%2.BAT
  158. echo if "%%1"=="" %%0 %0 .return .retain>>%2.BAT
  159. echo if "%%1"=="*" %%0 %0 .list .retain>>%2.BAT
  160. echo if not "%%3"==".retain" goto Retain>>%2.BAT
  161. if "%3"=="" goto StoreNoPath
  162. if "%3"==".empty" if not "%1"==".recreate" goto StoreNoPath
  163.  rem %0 stores the current RETAIN.BAT file name (& eventually called path),
  164.  rem which may be renamed, into RETURN.BAT
  165.  rem %2 stores the name and location of the alternate RETURN file into it the
  166.  rem first time it is being built. This information is valid to use instead of
  167.  rem %%0 (the return file call) _as l